package com.selangshiwo.sina.result;

import java.util.List;
//import java.util.Map;
//
//import org.apache.http.Header;
//import org.apache.http.HttpResponse;
//import org.apache.http.client.methods.HttpGet;
//import org.apache.http.util.EntityUtils;
//
//import com.alibaba.fastjson.JSONObject;
//import com.selangshiwo.Util;
//import com.selangshiwo.sina.httpclient.SinaHttpClient;
//import com.selangshiwo.sina.requst.RequestPack;

public class SSOLoginResultPack extends ResultPack{
	
	private List<String> urlList = null ;
	private String weiboUrl = "";
	
	public void initAfter() throws Exception{
		String context = this.getContext() ;
		//省事，不取其他的几个url了，直接取出weibo转向地址，好像不可以省事
		/*
		String key1 = "sinaSSOController.setCrossDomainUrlList";
		String key2 = ");}";
		String strResult = context.substring(context.indexOf(key1) + key1.length() + 1 , context.length());	//第一次截取
		strResult = strResult.substring(0 , strResult.indexOf(key2));
		Map<String , ?> json = (Map<String, ?>) JSONObject.parse(strResult) ;
		List<String> urls = (List)json.get("arrURL");
		for(int len = urls.size() -1 ; len >= 0 ; len --){		//偷懒
			//get
			String url = urls.get(len) ;
			url += "&action=login";
			url += "&callback=sinaSSOController.doCrossDomainCallBack";
			url += "&client=ssologin.js(v1.3.14)";
			url += "&scriptId=ssoscript" + len;
			//log("RESULT->" + get(url));
			//发送get请求
			HttpGet get = (HttpGet) new RequestPack("get",url).getHttpMethod() ;
//				HttpGet get = new HttpGet(url);
			HttpResponse response = SinaHttpClient.getInstance().execute(get);
//			Header[] header = response.getAllHeaders();
//			for(int h = header.length - 1; h >= 0 ; h --){
//				Util.debug(header[h].getName() + " : " + header[h].getValue());
//			}
			//Util.debug(Util.entity2String(response.getEntity()));
			get.abort() ;
		}
		*/
		String key1 = "location.replace(";
		String key2 = ");";
		String strWeiboUrl = context.substring(context.indexOf(key1) + key1.length() + 1 , context.length());	//第一次截取
		strWeiboUrl = strWeiboUrl.substring(0 , strWeiboUrl.indexOf(key2) - 1 );
		
		//微博认证地址
		this.weiboUrl = strWeiboUrl ;
		
	}
	
	public String getWeiboUrl(){
		return this.weiboUrl ;
	}
	
	public List<String> getUrlList(){
		return this.urlList ;
	}
}
